package girafPlace.Admin;

import girafPlace.Admin.Password;
import girafPlace.lib.Admin;
import girafPlace.sql.AdminSQL;

import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;



public class LoginUI { 
	JFrame mainui;
	private JTextField textField;
	private JPasswordField passwordField;
	private final Action action = new SwingAction();

    public LoginUI() {
    	mainui = new JFrame("GirafPlace");
    	mainui.setVisible(true);
    	mainui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	mainui.setSize(new Dimension(800, 160));
    	mainui.getContentPane().setSize(new Dimension(80, 80));
    	mainui.getContentPane().setLayout(null);
    	
    	JPanel panel = new JPanel();
    	panel.setBounds(100, 50, 600, 60);
    	mainui.getContentPane().add(panel);
    	panel.setLayout(null);
    	
    	JLabel lblEmail = new JLabel("Email:");
    	lblEmail.setBounds(56, 22, 67, 15);
    	panel.add(lblEmail);
    	
    	textField = new JTextField();
    	textField.setBounds(102, 20, 114, 19);
    	panel.add(textField);
    	textField.setColumns(10);
    	
    	JLabel lblPassword = new JLabel("Password:");
    	lblPassword.setBounds(242, 22, 83, 15);
    	panel.add(lblPassword);
    	
    	JButton btnLogin = new JButton("Login");
    	btnLogin.setAction(action);
    	btnLogin.setBounds(471, 17, 117, 25);
    	panel.add(btnLogin);
    	
    	passwordField = new JPasswordField();
    	passwordField.setBounds(322, 20, 114, 17);
    	panel.add(passwordField);
    	
    	JLabel lblVelkommenTilGirafplace = new JLabel("Velkommen til GirafPlace Administrations Panelet! Angiv din email og dit password for at logge ind.");
    	lblVelkommenTilGirafplace.setBounds(45, 23, 711, 15);
    	mainui.getContentPane().add(lblVelkommenTilGirafplace);
    	
    	mainui.repaint();

        
    }
	private class SwingAction extends AbstractAction {
		/**
		 * 
		 */
		private static final long serialVersionUID = 358643269855640368L;
		public SwingAction() {
			putValue(NAME, "Login");
			putValue(SHORT_DESCRIPTION, "Logs in.");
		}
		public void actionPerformed(ActionEvent e) {
			AdminSQL usersql = new AdminSQL();
			Admin collect = new Admin();
			Admin user = new Admin();
			Password pass = new Password();
			
			collect.setEmail(textField.getText());
			collect.setPassword(pass.getpass(passwordField.getPassword()));
		
			user = usersql.verify(collect);
			
			
			
			if(user!=null){
				System.out.println("1");
				@SuppressWarnings("unused")
				AdminUI adminui = new AdminUI(user);
				System.out.println("2");

			}	
		}
	}
}
